﻿using Sidvall.UI.Entities;
using System.Windows.Controls;
using System.Windows.Input;

namespace Sidvall.UI.Wpf.Controls
{
    public partial class PageItemControl : UserControl
    {
        #region Public Members

        #region PageCommand

        public static readonly System.Windows.DependencyProperty PageCommandProperty = System.Windows.DependencyProperty.Register(nameof(PageCommand), typeof(ICommand), typeof(PageItemControl),
            new System.Windows.FrameworkPropertyMetadata(null, System.Windows.FrameworkPropertyMetadataOptions.None, OnPageCommandChanged, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public ICommand PageCommand
        {
            get
            {
                return GetValue(PageCommandProperty) as ICommand;
            }
            set
            {
                SetValue(PageCommandProperty, value);
            }
        }
        private static void OnPageCommandChanged(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e)
        {
        }

        #endregion
        #region PageCommandParameter

        public static readonly System.Windows.DependencyProperty PageCommandParameterProperty = System.Windows.DependencyProperty.Register(nameof(PageCommandParameter), typeof(object), typeof(PageItemControl),
            new System.Windows.FrameworkPropertyMetadata(null, System.Windows.FrameworkPropertyMetadataOptions.None, PageCommandParameterChanged, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public object PageCommandParameter
        {
            get
            {
                return GetValue(PageCommandParameterProperty);
            }
            set
            {
                SetValue(PageCommandParameterProperty, value);
            }
        }
        private static void PageCommandParameterChanged(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e)
        {
        }

        #endregion
        #region PageItem

        public static readonly System.Windows.DependencyProperty PageItemProperty = System.Windows.DependencyProperty.Register(nameof(PageItem), typeof(PageItem), typeof(PageItemControl),
            new System.Windows.FrameworkPropertyMetadata(null, System.Windows.FrameworkPropertyMetadataOptions.None, OnPageItemChanged, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public PageItem PageItem
        {
            get
            {
                return GetValue(PageItemProperty) as PageItem;
            }
            set
            {
                SetValue(PageItemProperty, value);
            }
        }
        private static void OnPageItemChanged(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e)
        {
        }

        #endregion

        #endregion
        #region Private Members

        #region OnPageChanged

        private void OnPageChanged()
        {
            if (this.PageCommand == null)
                return;
            if (!this.PageCommand.CanExecute(this.PageCommandParameter))
                return;
            this.PageCommand.Execute(this.PageCommandParameter);
        }

        #endregion

        #endregion
        #region Events

        #region OnPageButtonClick

        private void PageFirstButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (!this.PageItem.CanPageBack)
                return;
            this.PageItem.CurrentPage = 1;
            OnPageChanged();
        }
        private void PageBackButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (!this.PageItem.CanPageBack)
                return;
            this.PageItem.CurrentPage -= 1;
            OnPageChanged();
        }
        private void PageForwardButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (!this.PageItem.CanPageForward)
                return;
            this.PageItem.CurrentPage += 1;
            OnPageChanged();
        }
        private void PageLastButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (!this.PageItem.CanPageForward)
                return;
            this.PageItem.CurrentPage = this.PageItem.PageCount;
            OnPageChanged();
        }

        #endregion
        #region OnPageItemStartButtonClick

        private void PageItemStart1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.PageItem.PageItemStart1 == null)
                return;
            this.PageItem.CurrentPage = this.PageItem.PageItemStart1.Value;
            OnPageChanged();
        }
        private void PageItemStart2_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.PageItem.PageItemStart2 == null)
                return;
            this.PageItem.CurrentPage = this.PageItem.PageItemStart2.Value;
            OnPageChanged();
        }
        private void PageItemStart3_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.PageItem.PageItemStart3 == null)
                return;
            this.PageItem.CurrentPage = this.PageItem.PageItemStart3.Value;
            OnPageChanged();
        }
        private void PageItemStart4_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.PageItem.PageItemStart4 == null)
                return;
            this.PageItem.CurrentPage = this.PageItem.PageItemStart4.Value;
            OnPageChanged();
        }

        #endregion
        #region OnPageItemPreviousButtonClick

        private void PageItemPrevious1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.PageItem.PageItemPrevious1 == null)
                return;
            this.PageItem.CurrentPage = this.PageItem.PageItemPrevious1.Value;
            OnPageChanged();
        }
        private void PageItemPrevious2_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.PageItem.PageItemPrevious2 == null)
                return;
            this.PageItem.CurrentPage = this.PageItem.PageItemPrevious2.Value;
            OnPageChanged();
        }
        private void PageItemPrevious3_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.PageItem.PageItemPrevious3 == null)
                return;
            this.PageItem.CurrentPage = this.PageItem.PageItemPrevious3.Value;
            OnPageChanged();
        }
        private void PageItemPrevious4_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.PageItem.PageItemPrevious4 == null)
                return;
            this.PageItem.CurrentPage = this.PageItem.PageItemPrevious4.Value;
            OnPageChanged();
        }

        #endregion
        #region OnPageItemNextButtonClick

        private void PageItemNext1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.PageItem.PageItemNext1 == null)
                return;
            this.PageItem.CurrentPage = this.PageItem.PageItemNext1.Value;
            OnPageChanged();
        }
        private void PageItemNext2_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.PageItem.PageItemNext2 == null)
                return;
            this.PageItem.CurrentPage = this.PageItem.PageItemNext2.Value;
            OnPageChanged();
        }
        private void PageItemNext3_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.PageItem.PageItemNext3 == null)
                return;
            this.PageItem.CurrentPage = this.PageItem.PageItemNext3.Value;
            OnPageChanged();
        }
        private void PageItemNext4_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.PageItem.PageItemNext4 == null)
                return;
            this.PageItem.CurrentPage = this.PageItem.PageItemNext4.Value;
            OnPageChanged();
        }

        #endregion
        #region OnPageItemEndButtonClick

        private void PageItemEnd1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.PageItem.PageItemEnd1 == null)
                return;
            this.PageItem.CurrentPage = this.PageItem.PageItemEnd1.Value;
            OnPageChanged();
        }
        private void PageItemEnd2_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.PageItem.PageItemEnd2 == null)
                return;
            this.PageItem.CurrentPage = this.PageItem.PageItemEnd2.Value;
            OnPageChanged();
        }
        private void PageItemEnd3_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.PageItem.PageItemEnd3 == null)
                return;
            this.PageItem.CurrentPage = this.PageItem.PageItemEnd3.Value;
            OnPageChanged();
        }
        private void PageItemEnd4_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.PageItem.PageItemEnd4 == null)
                return;
            this.PageItem.CurrentPage = this.PageItem.PageItemEnd4.Value;
            OnPageChanged();
        }

        #endregion

        #endregion
        #region Constructors

        public PageItemControl()
        {
            InitializeComponent();
        }

        #endregion
    }
}
